package com.meisiabc.app.mybatis;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Scanner;

public class CodeGenerator {
    public static String scanner(String tip) {
        System.out.println(new StringBuilder().append("请输入" + tip + "：").toString());

        Scanner scanner = new Scanner(System.in);

        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }

        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        String projectPath = System.getProperty("user.dir");

        AutoGenerator mpg = new AutoGenerator();

        mpg.setGlobalConfig(new GlobalConfig()
                .setOutputDir(projectPath + "/meisi-app/src/main/java")
                .setOpen(false)
                .setAuthor("mybatis plus")
                .setIdType(IdType.AUTO)
                .setFileOverride(true)
                .setServiceImplName("%sManager"));

        mpg.setDataSource(new DataSourceConfig()
                .setDriverName("com.mysql.jdbc.Driver")
                .setUrl("jdbc:mysql://62.234.115.218:3306/meisi?useUnicode=true&useSSL=false&characterEncoding=utf8")
                .setUsername("root")
                .setPassword("SUGAbYPpI05l7W7@#$%"));

        mpg.setPackageInfo(new PackageConfig()
                .setParent("com.meisiabc.app.repository.db")
                .setServiceImpl("manager"));


//        mpg.setCfg(new InjectionConfig() {
//            @Override
//            public void initMap() {
//            }
//        }.setFileOutConfigList(Arrays.asList(new FileOutConfig("/mybatis-templates/mapper.xml.ftl") {
//            @Override
//            public String outputFile(TableInfo tableInfo) {
//                return projectPath + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
//            }
//        })));


        mpg.setStrategy(new StrategyConfig()
                        .setNaming(NamingStrategy.underline_to_camel)
                        .setColumnNaming(NamingStrategy.underline_to_camel)
                        .setEntityLombokModel(true)
                .setInclude(scanner("表名"))
//                        .setInclude("admin",
//                                "curriculum_schedule",
//                                "free_audition",
//                                "hour_rate",
//                                "leave_request",
//                                "lesson",
//                                "lesson_level",
//                                "level",
//                                "period",
//                                "progress_report",
//                                "purchase_course",
//                                "recommend",
//                                "recommend_msg",
//                                "recommend_record",
//                                "send_msg_log",
//                                "student",
//                                "teacher",
//                                "teacher_period",
//                                "teacher_period_used",
//                                "test_report",
//                                "trylistenmanage",
//                                "wechat_app_session")
//                .setLogicDeleteFieldName("is_del")
//                .setVersionFieldName("version")
        );

        mpg.setTemplate(new TemplateConfig()
                .setEntity("/mybatis-templates/entity.java")
                .setMapper("/mybatis-templates/mapper.java")
                .setController(null)
                .setService(null)
                .setServiceImpl("/mybatis-templates/serviceImpl.java"));

        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}